# app/models/models.py

from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, BigInteger
from sqlalchemy.orm import relationship
from app.db.base import Base

class Job(Base):
    __tablename__ = 'jobs'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('users.name_id'), index=True, nullable=False)
    job_id = Column(Integer, nullable=False, unique=True)
    video_url = Column(String(500))  # 存储OSS视频链接
    audio_url = Column(String(500))  # 存储音频链接
    voice_id = Column(String(255), nullable=True)  # 语音ID - 修改为String类型支持UUID
    voice_type = Column(String(100), nullable=True)  # 语音类型
    text = Column(String(10000), nullable=True)  # 文本内容
    title = Column(String(255), nullable=True)  # 标题
    only_generate_audio = Column(Boolean, default=False)  # 是否只生成音频
    image_url = Column(String(500))  # 存储第一帧图片的OSS链接
    found_time = Column(Integer, nullable=False)  # 创建数据的时间戳（秒）
    video_time = Column(Integer, nullable=True)   # 视频生成完毕的时间戳（秒）
    video_duration = Column(Float, nullable=True) # 视频时长（秒）
    status = Column(Integer, nullable=False, default=3)  # 表示作品状态，默认值为3
    message = Column(String(1000), nullable=True,default="")  # 表示状态信息，存储字符串
    expire_time = Column(Integer, nullable=True) # 到期时间
    task_id = Column(String(255), nullable=True)  # 存储优速AI的taskId，用于回调通知
    user = relationship("User", back_populates="jobs")

# V2版本作品专用表
class JobV2(Base):
    __tablename__ = 'jobs_v2'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('users.name_id'), index=True, nullable=False)
    job_id = Column(Integer, nullable=False, unique=True)  # 外部API返回的jobId
    video_url = Column(String(500))  # 存储TOS视频链接
    audio_url = Column(String(500))  # 存储音频链接（V2版本通常为空）
    voice_id = Column(String(255), nullable=True)  # 语音ID（UUID格式）
    voice_type = Column(String(100), nullable=True)  # 语音类型（pro/lite）
    text = Column(String(10000), nullable=True)  # 文本内容
    title = Column(String(255), nullable=True)  # 标题
    only_generate_audio = Column(Boolean, default=False)  # V2版本固定为False
    image_url = Column(String(500))  # 存储第一帧图片的OSS链接
    found_time = Column(Integer, nullable=False)  # 创建数据的时间戳（秒）
    video_time = Column(Integer, nullable=True)   # 视频生成完毕的时间戳（秒）
    video_duration = Column(Float, nullable=True) # 视频时长（秒）
    status = Column(Integer, nullable=False, default=3)  # 表示作品状态，默认值为3
    message = Column(String(1000), nullable=True, default="")  # 表示状态信息
    expire_time = Column(Integer, nullable=True) # 到期时间
    task_id = Column(String(255), nullable=True)  # 存储优速AI的taskId，用于回调通知
    original_video_url = Column(String(500), nullable=True)  # 存储原始的personUrl
    external_video_url = Column(String(500), nullable=True)  # 存储外部API返回的视频URL
    user = relationship("User", back_populates="jobs_v2")
    
class Voice(Base):
    __tablename__ = 'voices'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('users.name_id'), nullable=False)
    voice_id = Column(Integer, nullable=False, unique=True)
    type = Column(Integer, nullable=False)
    voice_type = Column(Integer, nullable=False)
    title = Column(String(255), nullable=False)
    audio_url = Column(String(500), nullable=False)
    task_id = Column(String(255), nullable=True)  # 存储优速AI的taskId，用于回调通知
    speaker_id = Column(String(255), nullable=True)  # 存储外部API回调中的speakerId
    v2_voice_type = Column(String(10), nullable=True)  # 存储V2版本的voiceType参数（pro/lite）
    user = relationship("User", back_populates="voices")
    time = Column(Integer, nullable=False)

class User(Base):
    __tablename__ = 'users'

    name_id = Column(String(255), primary_key=True)
    videos = relationship("Video", back_populates="user")
    voices = relationship("Voice", back_populates="user")
    jobs = relationship("Job", back_populates="user")
    jobs_v2 = relationship("JobV2", back_populates="user")  # 新增V2作品关系

class Video(Base):
    __tablename__ = 'video'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('users.name_id'), nullable=False)
    video_url = Column(String(500), nullable=False)
    title = Column(String(255), nullable=False)
    time = Column(Integer, nullable=False)  # 存储当前时间戳（秒）
    user = relationship("User", back_populates="videos")

# 内部系统使用的表
class InternalUser(Base):
    __tablename__ = 'internal_users'

    name_id = Column(String(255), primary_key=True)
    internal_videos = relationship("InternalVideo", back_populates="user")
    internal_voices = relationship("InternalVoice", back_populates="user")
    internal_jobs = relationship("InternalJob", back_populates="user")

class InternalVideo(Base):
    __tablename__ = 'internal_videos'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('internal_users.name_id'), nullable=False)
    video_url = Column(String(500), nullable=False)
    title = Column(String(255), nullable=False)
    time = Column(Integer, nullable=False)  # 存储当前时间戳（秒）
    job_id = Column(String(50), nullable=False, unique=True)  # 随机生成的job_id
    user = relationship("InternalUser", back_populates="internal_videos")

class InternalVoice(Base):
    __tablename__ = 'internal_voices'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('internal_users.name_id'), nullable=False)
    voice_id = Column(Integer, nullable=False, unique=True)
    type = Column(Integer, nullable=False)
    voice_type = Column(Integer, nullable=False)
    title = Column(String(255), nullable=False)
    audio_url = Column(String(500), nullable=False)
    user = relationship("InternalUser", back_populates="internal_voices")
    time = Column(Integer, nullable=False)

class InternalJob(Base):
    __tablename__ = 'internal_jobs'

    id = Column(Integer, primary_key=True, index=True)
    name_id = Column(String(255), ForeignKey('internal_users.name_id'), index=True, nullable=False)
    job_id = Column(BigInteger, nullable=False, unique=True)
    video_job_id = Column(String(50), nullable=True)  # 关联到InternalVideo的job_id
    video_url = Column(String(500))  # 存储OSS视频链接
    audio_url = Column(String(500))  # 存储音频链接
    voice_id = Column(Integer, nullable=True)  # 语音ID
    voice_type = Column(String(100), nullable=True)  # 语音类型
    text = Column(String(10000), nullable=True)  # 文本内容
    title = Column(String(255), nullable=True)  # 标题
    only_generate_audio = Column(Boolean, default=False)  # 是否只生成音频
    image_url = Column(String(500))  # 存储第一帧图片的OSS链接
    found_time = Column(Integer, nullable=False)  # 创建数据的时间戳（秒）
    video_time = Column(Integer, nullable=True)   # 视频生成完毕的时间戳（秒）
    video_duration = Column(Float, nullable=True) # 视频时长（秒）
    status = Column(Integer, nullable=False, default=3)  # 表示作品状态，默认值为3
    message = Column(String(1000), nullable=True,default="string")  # 表示状态信息，存储字符串
    expire_time = Column(Integer, nullable=True) # 到期时间
    user = relationship("InternalUser", back_populates="internal_jobs")